#!/usr/bin/env python3
#-*- coding:utf-8 -*-

import matplotlib.pyplot as plt
import numpy as np
import random

title = 'Weighted IPC Speedup'
title_str = '加权IPC加速比'

def mean(data):
    mean = sum(data) / 5
    return mean


# 数据
test_cases = ['4M', '1C3M', '2C2M', '3C1M', '4C', 'AVG']
bingo = [random.randint(4, 15) for _ in range(5)] # 规则模式
spp = [random.randint(5, 16) for _ in range(5)] # 复杂模式
mlop = [random.randint(7, 17) for _ in range(5)] # 机器学习
pythia = [random.randint(8, 17) for _ in range(5)] # 强化学习
crlp = [random.randint(9, 22) for _ in range(5)] # 本文

print("Bingo在各工作负载上的", title_str, "为", bingo, sep='',)
print("SPP在各工作负载上的", title_str, "为", spp, sep='')
print("MLOP在各工作负载上的", title_str, "为", mlop, sep='')
print("Pythia在各工作负载上的", title_str, "为",pythia, sep='')
print("CRLP在各工作负载上的", title_str, "为", crlp, sep='')

avg_bingo = mean(bingo)
avg_spp = mean(spp)
avg_mlop = mean(mlop)
avg_pythia = mean(pythia)
avg_crlp = mean(crlp)

bingo += [avg_bingo]
spp += [avg_spp]
mlop += [avg_mlop]
pythia += [avg_pythia]
crlp += [avg_crlp]

# 各个预取器的覆盖率分别为10.7%，11.2%，5.7%，4.0%，5.3%
print("各个预取器的平均", title_str, "分别为", round(avg_bingo, 1), "%，", round(avg_spp, 1), "%，", round(avg_mlop, 1), "%，", round(avg_pythia, 1), "%，", round(avg_crlp, 1), "%。", sep='')

# CRLP的覆盖率相对于Bingo, SPP, MLOP, Pythia分别提升了10.7 %, 11.2 %, 5.7, 4.0 %
print("CRLP的", title_str, "相对于Bingo、SPP、MLOP和Pythia分别提升了", 
    round((avg_crlp-avg_bingo)*100/avg_bingo, 1), "%，",
    round((avg_crlp-avg_spp)*100/avg_spp, 1), "%，",
    round((avg_crlp-avg_mlop)*100/avg_mlop, 1), "%，",
    round((avg_crlp-avg_pythia)*100/avg_pythia, 1), "%。", sep='')

x = np.arange(len(test_cases))  # X轴位置
width = 0.15  # 柱子宽度

# 绘制柱状图
plt.bar(x - 1.5*width, bingo, width, label='Bingo', color='blue')
plt.bar(x - 0.5*width, spp, width, label='SPP', color='brown')
plt.bar(x + 0.5*width, mlop, width, label='MLOP', color='red')
plt.bar(x + 1.5*width, pythia, width, label='Pythia', color='orange')
plt.bar(x + 2.5*width, crlp, width, label='CRLP', color='green')


# 添加标签和标题
# plt.xlabel('Traces')
plt.ylabel(title + ' (%)')
plt.title(title)
plt.xticks(x, test_cases, rotation=45)
# plt.legend(loc='upper left')
plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.2), ncol=5)

# 显示图表
plt.tight_layout()
plt.show()
